/*
 * Yguan - Yui Gwt Unified ANimation
 *
 * Copyright (c) 2009, Yves Cuillerdier
 * All rights reserved.
 * 
 * This software is licensed under the new BSD License:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 * 
 * Neither the name of the author nor the names of its contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * @version 0.1.0
 */
package gyraf.wrapper.client;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Element;

/**
 * The animation module allows effects to be added to HTMLElements.
 * <p>
 * YUI provides four Javascript classes for the animations (Anim, ColorAnim,
 * Motion and Scroll). For the purpose of this GWT wrapper, all four are made a
 * single java class. The Animation object build depends of the factory method
 * called.
 */
public class YuiAnimator extends JavaScriptObject {

	/** Mandatory constructor */
	protected YuiAnimator() {
		// Mandatory constructor
	}

	/**
	 * Starts the animation by registering it with the animation manager.
	 */
	public final native void animate() /*-{
		this.animate();
	}-*/;

	/**
	 * Checks whether the element is currently animated.
	 * 
	 * @return current value of isAnimated.
	 */
	public final native boolean isAnimated() /*-{
	       return this.isAnimated();
	   }-*/;

	/**
	 * Stops the animation. Normally called by AnimMgr when animation completes.
	 * 
	 * @param finish
	 *            (optional) If true, animation will jump to final frame.
	 */
	public final native void stop(boolean finish) /*-{
	   	this.stop(finish);
	   }-*/;

	/**
	 * Stops the animation and jump to final frame. Normally called by AnimMgr
	 * when animation completes.
	 */
	public final native void stop() /*-{
		this.stop(true);
	}-*/;

	/****************************************************************
	 * Anim subclass for integer parameters transitions.
	 * <p>
	 * Usage:
	 * <code>var myAnim = new Y.ValueAnim(el, { width: { from: 100, to: 50, unit:'%' } }, 1, Y.Easing.easeOut);</code>
	 */

	/**
	 * Factory to create attribute animation
	 * 
	 * @param el
	 *            Reference to the element that will be animated
	 * @param attributes
	 *            The attribute(s) to be animated. Each attribute is an object
	 *            with at minimum a "to" or "by" member defined. Additional
	 *            optional members are "from" (defaults to current value),
	 *            "units" (defaults to "px"). All attribute names use camelCase.
	 * @param duration
	 *            (optional, defaults to 1 second) Length of animation (frames
	 *            or seconds), defaults to time-based
	 * @param easing
	 *            (optional, defaults to YAHOO.util.Easing.easeNone) Computes
	 *            the values that are applied to the attributes per frame
	 *            (generally a YAHOO.util.Easing method)
	 */
	public static native YuiAnimator makeAttributeAnimation(Element el,
			JavaScriptObject attributes, double duration,
			JavaScriptObject easing) /*-{
		if (easing==null) {
			if (duration==0) {
				return new $wnd.YAHOO.util.Anim(el,attributes);
			}
			return new $wnd.YAHOO.util.Anim(el,attributes,duration);
		}
		return new $wnd.YAHOO.util.Anim(el,attributes,duration,easing);
	}-*/;

	/**
	 * Factory to create attribute animation
	 * 
	 * @param el
	 *            Id of the element that will be animated
	 * @param attributes
	 *            The attribute(s) to be animated. Each attribute is an object
	 *            with at minimum a "to" or "by" member defined. Additional
	 *            optional members are "from" (defaults to current value),
	 *            "units" (defaults to "px"). All attribute names use camelCase.
	 * @param duration
	 *            (optional, defaults to 1 second) Length of animation (frames
	 *            or seconds), defaults to time-based
	 * @param easing
	 *            (optional, defaults to YAHOO.util.Easing.easeNone) Computes
	 *            the values that are applied to the attributes per frame
	 *            (generally a YAHOO.util.Easing method)
	 */
	public static native YuiAnimator makeAttributeAnimation(String el,
			JavaScriptObject attributes, double duration,
			JavaScriptObject easing) /*-{
		if (easing==null) {
			if (duration==0) {
				return new $wnd.YAHOO.util.Anim(el,attributes);
			}
			return new $wnd.YAHOO.util.Anim(el,attributes,duration);
		}
		return new $wnd.YAHOO.util.Anim(el,attributes,duration,easing);
	}-*/;

	/****************************************************************
	 * Anim subclass for color transitions.
	 * <p>
	 * Usage: <code>var myAnim = new Y.ColorAnim(el, { backgroundColor: { from: '#FF0000', to: '#FFFFFF' } }, 1, Y.Easing.easeOut);</code>
	 * Color values can be specified with either 112233, #112233, [255,255,255],
	 * or rgb(255,255,255)
	 */
	/**
	 * Factory to create color animation
	 * 
	 * @param el
	 *            Reference to the element that will be animated
	 * @param attributes
	 *            The attribute(s) to be animated. Each attribute is an object
	 *            with at minimum a "to" or "by" member defined. Additional
	 *            optional members are "from" (defaults to current value). All
	 *            attribute names use camelCase.
	 * @param duration
	 *            (optional, defaults to 1 second) Length of animation (frames
	 *            or seconds), defaults to time-based
	 * @param easing
	 *            (optional, defaults to YAHOO.util.Easing.easeNone) Computes
	 *            the values that are applied to the attributes per frame
	 *            (generally a YAHOO.util.Easing method)
	 */
	public static native YuiAnimator makeColorAnimation(Element el,
			JavaScriptObject attributes, double duration,
			JavaScriptObject easing) /*-{
		if (easing==null) {
			if (duration==0) {
				return new $wnd.YAHOO.util.ColorAnim(el,attributes);
			}
			return new $wnd.YAHOO.util.ColorAnim(el,attributes,duration);
		}
		return new $wnd.YAHOO.util.ColorAnim(el,attributes,duration,easing);
	}-*/;

	/**
	 * Factory to create color animation
	 * 
	 * @param el
	 *            ID of the element that will be animated
	 * @param attributes
	 *            The attribute(s) to be animated. Each attribute is an object
	 *            with at minimum a "to" or "by" member defined. Additional
	 *            optional members are "from" (defaults to current value). All
	 *            attribute names use camelCase.
	 * @param duration
	 *            (optional, defaults to 1 second) Length of animation (frames
	 *            or seconds), defaults to time-based
	 * @param easing
	 *            (optional, defaults to YAHOO.util.Easing.easeNone) Computes
	 *            the values that are applied to the attributes per frame
	 *            (generally a YAHOO.util.Easing method)
	 */
	public static native YuiAnimator makeColorAnimation(String el,
			JavaScriptObject attributes, double duration,
			JavaScriptObject easing) /*-{
		if (easing==null) {
			if (duration==0) {
				return new $wnd.YAHOO.util.ColorAnim(el,attributes);
			}
			return new $wnd.YAHOO.util.ColorAnim(el,attributes,duration);
		}
		return new $wnd.YAHOO.util.ColorAnim(el,attributes,duration,easing);
	}-*/;

	/**
	 * Anim subclass for moving elements along a path defined by the "points"
	 * member of "attributes". All "points" are arrays with x, y coordinates.
	 * <p>
	 * Usage:
	 * <code>var myAnim = new YAHOO.util.Motion(el, { points: { to: [800, 800] } }, 1, YAHOO.util.Easing.easeOut);</code>
	 */

	/**
	 * Factory to create Motion animation
	 * 
	 * @param el
	 *            Reference to the element that will be animated
	 * @param attributes
	 *            The attribute(s) to be animated. Each attribute is an object
	 *            with at minimum a "to" or "by" member defined. Additional
	 *            optional members are "from" and "control". All attribute names
	 *            use camelCase.
	 * @param duration
	 *            (optional, defaults to 1 second) Length of animation (frames
	 *            or seconds), defaults to time-based
	 * @param easing
	 *            (optional, defaults to YAHOO.util.Easing.easeNone) Computes
	 *            the values that are applied to the attributes per frame
	 *            (generally a YAHOO.util.Easing method)
	 */
	public static native YuiAnimator makeMotionAnimation(Element el,
			JavaScriptObject attributes, double duration,
			JavaScriptObject easing) /*-{
		if (easing==null) {
			if (duration==0) {
				return new $wnd.YAHOO.util.Motion(el,attributes);
			}
			return new $wnd.YAHOO.util.Motion(el,attributes,duration);
		}
		return new $wnd.YAHOO.util.Motion(el,attributes,duration,easing);
	}-*/;

	/**
	 * Factory to create Motion animation
	 * 
	 * @param el
	 *            Id of the element that will be animated
	 * @param attributes
	 *            The attribute(s) to be animated. Each attribute is an object
	 *            with at minimum a "to" or "by" member defined. Additional
	 *            optional members are "from" and "control". All attribute names
	 *            use camelCase.
	 * @param duration
	 *            (optional, defaults to 1 second) Length of animation (frames
	 *            or seconds), defaults to time-based
	 * @param easing
	 *            (optional, defaults to YAHOO.util.Easing.easeNone) Computes
	 *            the values that are applied to the attributes per frame
	 *            (generally a YAHOO.util.Easing method)
	 */
	public static native YuiAnimator makeMotionAnimation(String el,
			JavaScriptObject attributes, double duration,
			JavaScriptObject easing) /*-{
		if (easing==null) {
			if (duration==0) {
				return new $wnd.YAHOO.util.Motion(el,attributes);
			}
			return new $wnd.YAHOO.util.Motion(el,attributes,duration);
		}
		return new $wnd.YAHOO.util.Motion(el,attributes,duration,easing);
	}-*/;

	/**
	 * Anim subclass for scrolling elements to a position defined by the
	 * "scroll" member of "attributes". All "scroll" members are arrays with x,
	 * y scroll positions.
	 * <p>
	 * Usage:
	 * <code>var myAnim = new YAHOO.util.Scroll(el, { scroll: { to: [0, 800] } }, 1, YAHOO.util.Easing.easeOut);</code>
	 */

	/**
	 * Factory to create Scroll Animation
	 * 
	 * @param el
	 *            Reference to the element that will be animated The
	 *            attribute(s) to be animated. Each attribute is an object with
	 *            at minimum a "to" or "by" member defined. Additional optional
	 *            members are "from" and "control". All attribute names use
	 *            camelCase.
	 * @param duration
	 *            (optional, defaults to 1 second) Length of animation (frames
	 *            or seconds), defaults to time-based
	 * @param easing
	 *            (optional, defaults to YAHOO.util.Easing.easeNone) Computes
	 *            the values that are applied to the attributes per frame
	 *            (generally a YAHOO.util.Easing method)
	 */
	public static native YuiAnimator makeScrollAnimation(Element el,
			JavaScriptObject attributes, double duration,
			JavaScriptObject easing) /*-{
		if (easing==null) {
			if (duration==0) {
				return new $wnd.YAHOO.util.Scroll(el,attributes);
			}
			return new $wnd.YAHOO.util.Scroll(el,attributes,duration);
		}
		return new $wnd.YAHOO.util.Scroll(el,attributes,duration,easing);
	}-*/;

	/**
	 * Factory to create Scroll Animation
	 * 
	 * @param el
	 *            Id of the element that will be animated The attribute(s) to be
	 *            animated. Each attribute is an object with at minimum a "to"
	 *            or "by" member defined. Additional optional members are "from"
	 *            and "control". All attribute names use camelCase.
	 * @param duration
	 *            (optional, defaults to 1 second) Length of animation (frames
	 *            or seconds), defaults to time-based
	 * @param easing
	 *            (optional, defaults to YAHOO.util.Easing.easeNone) Computes
	 *            the values that are applied to the attributes per frame
	 *            (generally a YAHOO.util.Easing method)
	 */
	public static native YuiAnimator makeScrollAnimation(String el,
			JavaScriptObject attributes, double duration,
			JavaScriptObject easing) /*-{
		if (easing==null) {
			if (duration==0) {
				return new $wnd.YAHOO.util.Scroll(el,attributes);
			}
			return new $wnd.YAHOO.util.Scroll(el,attributes,duration);
		}
		return new $wnd.YAHOO.util.Scroll(el,attributes,duration,easing);
	}-*/;

}
